﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Verification.Mold
{
    /// <summary>
    /// 打乱列表顺序
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class UpsetUtils<T>
    {
        T[] item;
        //构造函数
        public UpsetUtils(T[] obj)
        {
            item = new T[obj.Length];
            for (int i = 0; i < obj.Length; i++)
            {
                item[i] = obj[i];
            }
        }

        public Type ShowType() { return typeof(T); } //返回类型
        public T[] GetItems() { return item; } //返回原数组

        //返回打乱顺序后的数组
        public T[] GetDisruptedItems()
        {
            //生成一个新数组：用于在之上计算和返回
            T[] temp;
            temp = new T[item.Length];
            for (int i = 0; i < temp.Length; i++) { temp[i] = item[i]; }

            //打乱数组中元素顺序
            Random rand = new Random(DateTime.Now.Millisecond);
            for (int i = 0; i < temp.Length; i++)
            {
                int x, y; T t;
                x = rand.Next(0, temp.Length);
                do
                {
                    y = rand.Next(0, temp.Length);
                } while (y == x);

                t = temp[x];
                temp[x] = temp[y];
                temp[y] = t;
            }
            int j = 0;
            for (int i = 0; i < temp.Length; i++)
            {
                if (temp[i].ToString() == item[i].ToString())
                {
                    j++;
                }

            }
            //如果相同就递归打乱直至不相同为止
            if (j == temp.Length)
                temp = GetDisruptedItems();
            return temp;
        }
    }
}
